package com.sunshine.lnuplus.utils;

import android.os.Build;

import androidx.annotation.RequiresApi;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

public class TimeHelper {
    private static Calendar calendar = Calendar.getInstance();
    private static int year = calendar.get(Calendar.YEAR);
    private static int month = calendar.get(Calendar.MONTH)+1;
    private static int day = calendar.get(Calendar.DAY_OF_MONTH);
    private static int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
    private static int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
    private static int hour = calendar.get(Calendar.HOUR_OF_DAY);
    private static int minute = calendar.get(Calendar.MINUTE);

    public static int getYear() {
        return year;
    }

    public static int getMonth() {
        return month;
    }

    public static int getDay() {
        return day;
    }

    public static int getDayOfWeek() {
        return dayOfWeek;
    }

    public static int getWeekOfYear() {
        return weekOfYear;
    }

    public static int getHour() {
        return hour;
    }

    public static int getMinute() {
        return minute;
    }

    public static int getThisWeek(int month,int day){
        Calendar startCalendar = Calendar.getInstance();
        int startWeek,thisWeek;

        //上学期
        if(month>8){
            startCalendar.set(Calendar.MONTH,month);
            startCalendar.set(Calendar.DAY_OF_MONTH,day);
            startWeek = startCalendar.get(Calendar.WEEK_OF_YEAR);
            //开学那天为周日
            if(startCalendar.get(Calendar.DAY_OF_WEEK)== Calendar.SUNDAY){
                startWeek--;
            }
            //今天为周日
            if(getDayOfWeek()==1){
                startWeek++;
            }
            thisWeek = getWeekOfYear()-startWeek+1;
            //上课周
            if(thisWeek>0){
                return thisWeek;
            }//放假
            else {
                return -1;
            }
        }else {
            startCalendar.set(Calendar.MONTH,month);
            startCalendar.set(Calendar.DAY_OF_MONTH,day);
            startWeek = startCalendar.get(Calendar.WEEK_OF_YEAR);
            if(startCalendar.get(Calendar.DAY_OF_WEEK)== Calendar.SUNDAY){
                startWeek--;
            }
            if(getDayOfWeek()==1){
                startWeek++;
            }
            thisWeek = getWeekOfYear()-startWeek+1;
            //上课周
            if(thisWeek>0){
                return thisWeek;
            }//放假
            else {
                return -1;
            }
        }
    }

    //获取今天周几信息
    public static String getTodayOfWeek(){
        int dayOfWeek = getDayOfWeek();
        switch (dayOfWeek){
            case 1:return "日";
            case 2:return "一";
            case 3:return "二";
            case 4:return "三";
            case 5:return "四";
            case 6:return "五";
            case 7:return "六";
            default:return null;
        }
    }

    //获取循环数组day的周几信息
    public static String getDayOfWeek(int day){
        switch (day){
            case 1:return "一";
            case 2:return "二";
            case 3:return "三";
            case 4:return "四";
            case 5:return "五";
            case 6:return "六";
            case 7:return "日";
            default:return null;
        }
    }

    public static int getCountDownTime(String time) {
        if(time.equals("")) return 0;
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        String today = getYear()+"-"+getMonth()+"-"+getDay()+" "+getHour()+":"+getMinute();
        /*天数差*/
        Date fromDate1 = null;
        Date toDate1 = null;
        try {
            fromDate1 = simpleFormat.parse(today);
            toDate1 = simpleFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long from1 = fromDate1.getTime();
        long to1 = toDate1.getTime();
        int days = (int) ((to1 - from1) / (1000 * 60 * 60 * 24));
        return days;
    }
}
